/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：RegisterDialog.java <br>
 * 创建时间：2013-5-21 下午4:07:07 <br>
 * 所属项目：GmudEX <br>
 *
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.meta;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.listener.SaveListener;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.R;
import lostland.gmud.exv2.community.GmudUser;

/**
 * 类名：RegisterDialog <br>
 * 说明：注册对话框
 *
 * @author 12548
 */
public class RegisterDialog extends Dialog {

	/**
	 * 用户名长度上限
	 */
	private static final int USERNAME_LENGTH_UPPERLIMIT = 10;

	/**
	 * 用户名长度下限
	 */
	private static final int USERNAME_LENGTH_LOWERLIMIT = 2;

	private Window mWindow;

	EditText etEmail;
	private EditText etUsername;
	private EditText etPassword;


	/**
	 * @param context
	 */
	public RegisterDialog(Context context) {
		super(context);
		mWindow = getWindow();
		mWindow.setBackgroundDrawable(new ColorDrawable(0));
		mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.gravity = Gravity.CENTER;
		lp.dimAmount = 0.0001f;
		mWindow.setAttributes(lp);
		setCanceledOnTouchOutside(true);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWindow.setContentView(R.layout.dialog_register);


		TextView tvTitle = (TextView) findViewById(R.id.textView1);
		tvTitle.setText("注册");

		etUsername = (EditText) findViewById(R.id.etUsername);
		etEmail = (EditText) findViewById(R.id.etEmail);
		etPassword = (EditText) findViewById(R.id.etPassword);

		Button btn = (Button) findViewById(R.id.btnRegister);
		btn.setText("注册");
		btn.setOnClickListener(new View.OnClickListener() {

			                       @Override
			                       public void onClick(View v) {
				                       final String usrname = etUsername.getText().toString().trim();

				                       if (usrname.length() < USERNAME_LENGTH_LOWERLIMIT
						                       || usrname.length() > USERNAME_LENGTH_UPPERLIMIT) {

					                       Toast.makeText(
							                       Game.getInstance(),
							                       String.format("用户名长度必须在%d到%d个字符之间！", USERNAME_LENGTH_LOWERLIMIT,
									                       USERNAME_LENGTH_UPPERLIMIT), Toast.LENGTH_SHORT).show();
					                       return;
				                       }

				                       GmudUser bu = new GmudUser();

				                       bu.setEmail(etEmail.getText().toString());
				                       bu.setUsername(usrname);
				                       bu.setNickName(usrname);
				                       bu.setPassword(etPassword.getText().toString());
// bu.setGold(1000);
				                       Toast.makeText(Game.getInstance(), "注册中......", Toast.LENGTH_SHORT).show();
				                       bu.signUp(Game.getInstance(), new SaveListener() {
					                       @Override
					                       public void onSuccess() {
						                       Toast.makeText(Game.getInstance(), "注册成功！", Toast.LENGTH_SHORT).show();
						                       Game.getInstance().popToRootScreen();
						                       dismiss();
					                       }

					                       @Override
					                       public void onFailure(int code, String msg) {
						                       Toast.makeText(Game.getInstance(), "注册失败！" + msg, Toast.LENGTH_SHORT)
								                       .show();
					                       }
				                       });
			                       }
		                       }
		);
	}


	/*
	 * （非 Javadoc）
	 * @see android.app.Dialog#cancel()
	 */
	@Override
	public void cancel() {
		super.cancel();
		Game.getInstance().popToRootScreen();
	}

}
